﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace Hzd.RegexHelper
{
    public class Helper
    {
        public static string GetHtml(string url, bool isrss)
        {
            string input = string.Empty;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.AllowAutoRedirect = true;
            request.AllowWriteStreamBuffering = true;
            request.Referer = "";
            request.Timeout = 10000;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
            HttpWebResponse response = null;
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Encoding encoding;
                    int num;
                    string characterSet = response.CharacterSet;
                    if (characterSet != "")
                    {
                        if (characterSet == "ISO-8859-1")
                        {
                            characterSet = "gb2312";
                        }
                        encoding = Encoding.GetEncoding(characterSet);
                    }
                    else
                    {
                        encoding = Encoding.Default;
                    }
                    Stream responseStream = response.GetResponseStream();
                    MemoryStream stream = new MemoryStream();
                    byte[] buffer = new byte[255];
                    for (num = responseStream.Read(buffer, 0, 255); num > 0; num = responseStream.Read(buffer, 0, 255))
                    {
                        stream.Write(buffer, 0, num);
                    }
                    responseStream.Close();
                    stream.Seek(0L, SeekOrigin.Begin);
                    StreamReader reader = new StreamReader(stream, encoding);
                    char[] chArray = new char[1024];
                    for (num = reader.Read(chArray, 0, 1024); num > 0; num = reader.Read(chArray, 0, 1024))
                    {
                        input = input + new string(chArray, 0, num);
                    }
                    Regex regex = null;
                    if (isrss)
                    {
                        regex = new Regex("(?<=encoding=\").*?(?=\")", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                    }
                    else
                    {
                        regex = new Regex("<meta[\\s\\S]+?charset=(.*)\"[\\s\\S]+?>", RegexOptions.Multiline | RegexOptions.IgnoreCase);
                    }
                    MatchCollection matchs = regex.Matches(input);
                    if (matchs.Count > 0)
                    {
                        string strA = null;
                        if (isrss)
                        {
                            strA = matchs[0].Result("$0");
                        }
                        else
                        {
                            strA = matchs[0].Result("$1");
                        }
                        if (string.Compare(strA, characterSet, true) != 0)
                        {
                            encoding = Encoding.GetEncoding(strA);
                            input = string.Empty;
                            stream.Seek(0L, SeekOrigin.Begin);
                            reader = new StreamReader(stream, encoding);
                            chArray = new char[255];
                            for (num = reader.Read(chArray, 0, 255); num > 0; num = reader.Read(chArray, 0, 255))
                            {
                                input = input + new string(chArray, 0, num);
                            }
                        }
                    }
                    reader.Close();
                    stream.Close();
                }
                return input;
            }
            catch (Exception exception)
            {
                Trace.TraceError(exception.ToString());
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
            return input;
        }

        public static string zregex(string html, string zq)
        {
            string str = null;
            try
            {
                MatchCollection matchs = Regex.Matches(html, zq, RegexOptions.IgnoreCase);
                Dictionary<string, int> dictionary = new Dictionary<string, int>();
                if (matchs.Count > 0)
                {
                    for (int i = 0; i < matchs.Count; i++)
                    {
                        if (i == 0)
                        {
                            try
                            {
                                dictionary.Add(matchs[i].Value, 0);
                                str = str + matchs[i].Value;
                            }
                            catch
                            {
                            }
                        }
                        else
                        {
                            try
                            {
                                dictionary.Add(matchs[i].Value, 0);
                                str = str + "===" + matchs[i].Value;
                            }
                            catch
                            {
                            }
                        }
                    }
                }
                dictionary = null;
            }
            catch (Exception exception)
            {
            }
            return str;
        }

        public static string[] getFinalResult(string input)
        {
            try
            {
                return Regex.Split(input, "===", RegexOptions.IgnoreCase);
            }
            catch
            {
                return Regex.Split("没有匹配项", "===", RegexOptions.IgnoreCase);
            }
        }
    }
}
